package com.gulimall.search.controller;

import com.gulimall.search.service.ProductSaveService;
import com.zlc.common.exception.BizCodeEnume;
import com.zlc.common.to.es.SkuEsModel;
import com.zlc.common.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search/save")
public class ElasticSearchController {

    @Resource
    private ProductSaveService productSaveService;

    //上架商品
    @PostMapping("/product")
    public R upProduct(@RequestBody List<SkuEsModel> skuEsModels){
        boolean b = false;
        try {
            b = productSaveService.upProduct(skuEsModels);
        } catch (IOException e) {
            return R.error(BizCodeEnume.UP_PRODUCT_EXCEPTION.getCode(),BizCodeEnume.UP_PRODUCT_EXCEPTION.getMessage());
        }
        if(!b){
            return R.ok();
        }else{
            return R.error(BizCodeEnume.UP_PRODUCT_EXCEPTION.getCode(),BizCodeEnume.UP_PRODUCT_EXCEPTION.getMessage());
        }
    }
}
